home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WPrefs.app / Font.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-14  |  11.7 KB  |  490 lines

  1. /* Font.c- text/font settings
  2.  * 
  3.  *  WPrefs - Window Maker Preferences Program
  4.  * 
  5.  *  Copyright (c) 1999 Alfredo K. Kojima
  6.  * 
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
  20.  *  USA.
  21.  */
  22.  
  23.  
  24. #include "WPrefs.h"
  25.  
  26. #include <proplist.h>
  27.  
  28. typedef struct _Panel {
  29.     WMFrame *frame;
  30.     char *sectionName;   
  31.  
  32.     char *description;
  33.  
  34.     CallbackRec callbacks;
  35.     
  36.     WMWindow *win;
  37.  
  38.  
  39.     WMLabel *prevL;
  40.  
  41.     WMFrame *langF;
  42.     WMPopUpButton *langP;
  43.         
  44.     /* single byte */
  45.     WMTextField *fontT;
  46.     WMButton *changeB;
  47.     
  48.     /* multibyte */
  49.     WMLabel *fsetL;
  50.     WMList *fsetLs;
  51.     
  52.     WMButton *addB;
  53.     WMButton *editB;
  54.     WMButton *remB;
  55.  
  56.     
  57.     WMFont *windowTitleFont;
  58.     WMFont *menuTitleFont;
  59.     WMFont *menuItemFont;
  60.     
  61.     
  62.     WMColor *white;
  63.     WMColor *black;
  64.     WMColor *light;
  65.     WMColor *dark;
  66.     
  67.     WMColor *back;
  68.  
  69.     Pixmap preview;
  70.     WMPixmap *previewPix;
  71. } _Panel;
  72.  
  73.  
  74.  
  75. #define ICON_FILE    "fonts"
  76.  
  77.  
  78.  
  79. static proplist_t DefaultWindowTitleFont = NULL;
  80. static proplist_t DefaultMenuTitleFont = NULL;
  81. static proplist_t DefaultMenuTextFont = NULL;
  82. static proplist_t DefaultIconTitleFont = NULL;
  83. static proplist_t DefaultClipTitleFont = NULL;
  84. static proplist_t DefaultDisplayFont = NULL;
  85.  
  86.  
  87.  
  88. static void
  89. drawMenuItem(WMScreen *scr, Display *dpy, Drawable d, 
  90.          int x, int y, int w, int h, 
  91.          GC light, GC dark, GC black, GC white,
  92.          WMFont *font, int fh, char *text)
  93. {
  94.     XFillRectangle(dpy, d, light, x, y, w, h);
  95.     
  96.     XDrawLine(dpy, d, black, x, y, x, y+h);
  97.     XDrawLine(dpy, d, black, x+w, y, x+w, y+h);
  98.     
  99.     XDrawLine(dpy, d, white, x+1, y, x+1, y+h-1);
  100.     XDrawLine(dpy, d, white, x+1, y, x+w-1, y);
  101.     
  102.     XDrawLine(dpy, d, dark, x+w-1, y+1, x+w-1, y+h-3);
  103.     XDrawLine(dpy, d, dark, x+1, y+h-2, x+w-1, y+h-2);
  104.     
  105.     XDrawLine(dpy, d, black, x, y+h-1, x+w, y+h-1);
  106.  
  107.     WMDrawString(scr, d, black, font, x + 5, y+(h-fh)/2, 
  108.          text, strlen(text));
  109. }
  110.  
  111.  
  112.  
  113. static void
  114. paintPreviewBox(Panel *panel)
  115. {
  116.     WMScreen *scr = WMWidgetScreen(panel->win);
  117.     Display *dpy = WMScreenDisplay(scr);
  118.     GC black = WMColorGC(panel->black);
  119.     GC white = WMColorGC(panel->white);
  120.     GC dark = WMColorGC(panel->dark);
  121.     GC light = WMColorGC(panel->light);
  122.     
  123.     
  124.     if (panel->preview == None) {
  125.     WMPixmap *pix;
  126.     
  127.     panel->preview = XCreatePixmap(dpy, WMWidgetXID(panel->win),
  128.                        240-4, 215-4, WMScreenDepth(scr));
  129.  
  130.     pix = WMCreatePixmapFromXPixmaps(scr, panel->preview, None,
  131.                      240-4, 215-4, WMScreenDepth(scr));
  132.  
  133.     WMSetLabelImage(panel->prevL, pix);
  134.     WMReleasePixmap(pix);
  135.     }
  136.     
  137.     XFillRectangle(dpy, panel->preview, WMColorGC(panel->back),
  138.            0, 0, 240-4, 215-4);
  139.  
  140.     /* window title */
  141.     {
  142.     int h, fh;
  143.     
  144.     fh = WMFontHeight(panel->windowTitleFont);
  145.     h = fh+6;
  146.     
  147.     XFillRectangle(dpy, panel->preview, black,
  148.                19, 19, 203, h+3);
  149.  
  150.     XDrawLine(dpy, panel->preview, light,
  151.           20, 20, 220, 20);
  152.     XDrawLine(dpy, panel->preview, light,
  153.           20, 20, 20, 20+h);
  154.  
  155.     XDrawLine(dpy, panel->preview, dark,
  156.           20, 20+h, 220, 20+h);    
  157.     XDrawLine(dpy, panel->preview, dark,
  158.           220, 20, 220, 20+h);
  159.     
  160.     WMDrawString(scr, panel->preview, white, panel->windowTitleFont,
  161.              20+(200-WMWidthOfString(panel->windowTitleFont, "Window Titlebar", 15))/2,
  162.              20+(h-fh)/2, "Window Titlebar", 15);
  163.     }
  164.     
  165.     /* menu title */
  166.     {
  167.     int h, h2, fh, fh2;
  168.     int i;
  169.     const int mx = 20;
  170.     const int my = 120;
  171.     const int mw = 100;
  172.  
  173.     
  174.     fh = WMFontHeight(panel->menuTitleFont);
  175.     h = fh+6;
  176.  
  177.     XFillRectangle(dpy, panel->preview, black,
  178.                mx-1, my-1, mw+3, h+3);
  179.  
  180.     XDrawLine(dpy, panel->preview, light,
  181.           mx, my, mx+mw, my);
  182.     XDrawLine(dpy, panel->preview, light,
  183.           mx, my, mx, my+h);
  184.  
  185.     XDrawLine(dpy, panel->preview, dark,
  186.           mx, my+h, mx+mw, my+h);    
  187.     XDrawLine(dpy, panel->preview, dark,
  188.           mx+mw, my, mx+mw, my+h);
  189.  
  190.     WMDrawString(scr, panel->preview, white, panel->menuTitleFont,
  191.              mx+5, my+(h-fh)/2, "Menu Title", 10);
  192.     
  193.     fh2 = WMFontHeight(panel->menuItemFont);
  194.     h2 = fh2+6;
  195.     
  196.     /* menu items */
  197.     for (i = 0; i < 4; i++) {
  198.         drawMenuItem(scr, dpy, panel->preview,
  199.              mx-1, my+2+h+i*h2, mw+2, h2,
  200.              light, dark, black, white, 
  201.              panel->menuItemFont, fh2,
  202.              "Menu Item");
  203.     }
  204.     }
  205.  
  206.  
  207.     WMRedisplayWidget(panel->prevL);
  208. }
  209.  
  210.  
  211.  
  212. static void
  213. showData(_Panel *panel)
  214. {
  215.     WMScreen *scr = WMWidgetScreen(panel->win);
  216.     char *str;
  217.     
  218.     str = GetStringForKey("WindowTitleFont");
  219.         
  220.     panel->windowTitleFont = WMCreateFont(scr, str);
  221.     
  222.     
  223.     str = GetStringForKey("MenuTitleFont");
  224.     
  225.     panel->menuTitleFont = WMCreateFont(scr, str);
  226.     
  227.     
  228.     str = GetStringForKey("MenuTextFont");
  229.     
  230.     panel->menuItemFont = WMCreateFont(scr, str);
  231.     
  232.     
  233.     
  234.     
  235.     paintPreviewBox(panel);
  236. }
  237.  
  238.  
  239. static void
  240. setLanguageType(Panel *p, Bool multiByte)
  241. {
  242.     if (multiByte) {
  243.     WMMapWidget(p->fsetL);
  244.     WMMapWidget(p->fsetLs);
  245.     WMMapWidget(p->addB);
  246.     WMMapWidget(p->editB);
  247.     WMMapWidget(p->remB);
  248.     
  249.     WMUnmapWidget(p->fontT);
  250.     WMUnmapWidget(p->changeB);
  251.     } else {
  252.     WMUnmapWidget(p->fsetL);
  253.     WMUnmapWidget(p->fsetLs);
  254.     WMUnmapWidget(p->addB);
  255.     WMUnmapWidget(p->editB);
  256.     WMUnmapWidget(p->remB);
  257.     
  258.     WMMapWidget(p->fontT);
  259.     WMMapWidget(p->changeB);
  260.     }
  261. }
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. static void
  269. readFontEncodings(Panel *panel)
  270. {
  271.     proplist_t pl = NULL;
  272.     char *path;
  273.     char *msg;
  274.  
  275.     path = WMPathForResourceOfType("font.data", NULL);
  276.     if (!path) {
  277.     msg = _("Could not locate font information file WPrefs.app/font.data");
  278.     goto error;
  279.     }
  280.     
  281.     pl = PLGetProplistWithPath(path);
  282.     if (!pl) {
  283.     msg = _("Could not read font information file WPrefs.app/font.data");
  284.     goto error;
  285.     } else {
  286.     int i;
  287.     proplist_t key = PLMakeString("Encodings");
  288.     proplist_t array;
  289.     WMMenuItem *mi;
  290.     
  291.     array = PLGetDictionaryEntry(pl, key);
  292.     PLRelease(key);
  293.     if (!array || !PLIsArray(array)) {
  294.         msg = _("Invalid data in font information file WPrefs.app/font.data.\n"
  295.             "Encodings data not found.");
  296.         goto error;
  297.     }
  298.  
  299.     WMAddPopUpButtonItem(panel->langP, _("- Custom -"));
  300.     
  301.     for (i = 0; i < PLGetNumberOfElements(array); i++) {
  302.         proplist_t item, str;
  303.  
  304.         item = PLGetArrayElement(array, i);
  305.         str = PLGetArrayElement(item, 0);
  306.         mi = WMAddPopUpButtonItem(panel->langP, PLGetString(str));
  307.         WMSetMenuItemRepresentedObject(mi, PLRetain(item));
  308.     }
  309.     
  310.     key = PLMakeString("WindowTitleFont");
  311.     DefaultWindowTitleFont = PLGetDictionaryEntry(pl, key);
  312.     PLRelease(key);
  313.     
  314.     key = PLMakeString("MenuTitleFont");
  315.     DefaultMenuTitleFont = PLGetDictionaryEntry(pl, key);
  316.     PLRelease(key);
  317.  
  318.     key = PLMakeString("MenuTextFont");
  319.     DefaultMenuTextFont = PLGetDictionaryEntry(pl, key);
  320.     PLRelease(key);
  321.     }
  322.  
  323.     PLRelease(pl);
  324.     return;
  325. error:
  326.     if (pl)
  327.     PLRelease(pl);
  328.  
  329.     WMRunAlertPanel(WMWidgetScreen(panel->win), panel->win, 
  330.             _("Error"), msg, _("OK"), NULL, NULL);
  331. }
  332.  
  333.  
  334.  
  335. static void
  336. changeLanguageAction(WMWidget *w, void *data)
  337. {
  338.     Panel *panel = (Panel*)data;
  339.     WMMenuItem *mi;
  340.     proplist_t pl, font;
  341.     char buffer[512];
  342.     
  343.     mi = WMGetPopUpButtonMenuItem(w, WMGetPopUpButtonSelectedItem(w));
  344.     pl = WMGetMenuItemRepresentedObject(mi);
  345.  
  346.     if (!pl) {
  347.     /* custom */
  348.     } else {
  349.     
  350.     }
  351. }
  352.  
  353.  
  354. static void
  355. createPanel(Panel *p)
  356. {    
  357.     _Panel *panel = (_Panel*)p;
  358.     WMScreen *scr = WMWidgetScreen(panel->win);
  359.     
  360.     panel->frame = WMCreateFrame(panel->win);
  361.     WMResizeWidget(panel->frame, FRAME_WIDTH, FRAME_HEIGHT);
  362.     WMMoveWidget(panel->frame, FRAME_LEFT, FRAME_TOP);
  363.     
  364.  
  365.     panel->prevL = WMCreateLabel(panel->frame);
  366.     WMResizeWidget(panel->prevL, 240, FRAME_HEIGHT-20);
  367.     WMMoveWidget(panel->prevL, 15, 10);
  368.     WMSetLabelRelief(panel->prevL, WRSunken);
  369.     WMSetLabelImagePosition(panel->prevL, WIPImageOnly);
  370.  
  371.     
  372.     /* language selection */
  373.     
  374.     panel->langF = WMCreateFrame(panel->frame);
  375.     WMResizeWidget(panel->langF, 245, 50);
  376.     WMMoveWidget(panel->langF, 265, 10);
  377.     WMSetFrameTitle(panel->langF, _("Default Font Sets"));
  378.  
  379.     panel->langP = WMCreatePopUpButton(panel->langF);
  380.     WMResizeWidget(panel->langP, 215, 20);
  381.     WMMoveWidget(panel->langP, 15, 20);
  382.  
  383.     WMMapSubwidgets(panel->langF);
  384.  
  385.     
  386.     /* multibyte */
  387.     panel->fsetL = WMCreateLabel(panel->frame);
  388.     WMResizeWidget(panel->fsetL, 205, 20);
  389.     WMMoveWidget(panel->fsetL, 215, 127);
  390.     WMSetLabelText(panel->fsetL, _("Font Set"));
  391.     WMSetLabelRelief(panel->fsetL, WRSunken);
  392.     WMSetLabelTextAlignment(panel->fsetL, WACenter);
  393.     {
  394.     WMFont *font;
  395.     WMColor *color;
  396.     
  397.     color = WMDarkGrayColor(scr);
  398.     font = WMBoldSystemFontOfSize(scr, 12);
  399.     
  400.     WMSetWidgetBackgroundColor(panel->fsetL, color);
  401.     WMSetLabelFont(panel->fsetL, font);
  402.     
  403.     WMReleaseFont(font);
  404.     WMReleaseColor(color);
  405.     
  406.     color = WMWhiteColor(scr);
  407.     WMSetLabelTextColor(panel->fsetL, color);
  408.     WMReleaseColor(color);
  409.     }
  410.  
  411.     panel->fsetLs = WMCreateList(panel->frame);
  412.     WMResizeWidget(panel->fsetLs, 205, 71);
  413.     WMMoveWidget(panel->fsetLs, 215, 149);
  414.     
  415.  
  416.     panel->addB = WMCreateCommandButton(panel->frame);
  417.     WMResizeWidget(panel->addB, 80, 24);
  418.     WMMoveWidget(panel->addB, 430, 127);
  419.     WMSetButtonText(panel->addB, _("Add..."));
  420.  
  421.     panel->editB = WMCreateCommandButton(panel->frame);
  422.     WMResizeWidget(panel->editB, 80, 24);
  423.     WMMoveWidget(panel->editB, 430, 161);
  424.     WMSetButtonText(panel->editB, _("Change..."));
  425.  
  426.     panel->remB = WMCreateCommandButton(panel->frame);
  427.     WMResizeWidget(panel->remB, 80, 24);
  428.     WMMoveWidget(panel->remB, 430, 195);
  429.     WMSetButtonText(panel->remB, _("Remove"));
  430.  
  431.     /* single byte */
  432.     panel->fontT = WMCreateTextField(panel->frame);
  433.     WMResizeWidget(panel->fontT, 240, 20);
  434.     WMMoveWidget(panel->fontT, 265, 130);
  435.     
  436.     panel->changeB = WMCreateCommandButton(panel->frame);
  437.     WMResizeWidget(panel->changeB, 104, 24);
  438.     WMMoveWidget(panel->changeB, 335, 160);
  439.     WMSetButtonText(panel->changeB, _("Change..."));
  440.  
  441.     
  442.     panel->black = WMBlackColor(scr);
  443.     panel->white = WMWhiteColor(scr);
  444.     panel->light = WMGrayColor(scr);
  445.     panel->dark = WMDarkGrayColor(scr);
  446.     panel->back = WMCreateRGBColor(scr, 0x5100, 0x5100, 0x7100, True); 
  447.     
  448. #if 0    
  449.     for (i = 0; Languages[i].language != NULL; i++) {
  450.     WMAddPopUpButtonItem(panel->langP, Languages[i].language);
  451.     }
  452.  
  453.     for (i = 0; Options[i].description != NULL; i++) {
  454.     WMAddListItem(panel->settingLs, Options[i].description);
  455.     }
  456. #endif
  457.     WMRealizeWidget(panel->frame);
  458.     WMMapSubwidgets(panel->frame);
  459.  
  460.     setLanguageType(panel, False);
  461.  
  462.     showData(panel);
  463.     
  464.     readFontEncodings(panel);
  465. }
  466.  
  467.  
  468.  
  469.  
  470. Panel*
  471. InitFont(WMScreen *scr, WMWindow *win)
  472. {
  473.     _Panel *panel;
  474.  
  475.     panel = wmalloc(sizeof(_Panel));
  476.     memset(panel, 0, sizeof(_Panel));
  477.  
  478.     panel->sectionName = _("Font Preferences");
  479.     panel->description = _("Font Configurations for Windows, Menus etc");
  480.  
  481.     panel->win = win;
  482.     
  483.     panel->callbacks.createWidgets = createPanel;
  484.     
  485.     AddSection(panel, ICON_FILE);
  486.     
  487.     return panel;
  488. }
  489.  
  490.